import { Button, Input, message } from 'antd'
import React, { useEffect, useState } from 'react'
import axios from 'axios'
import {useNavigate}from 'react-router-dom'
export default function Login() {
  const [username,setusername]=useState("")
         const navigate = useNavigate()
  useEffect(()=>{
    const victyToken = window.localStorage.getItem("victyToke")
    if(victyToken) {
      console.log("Success")
    }else{
      console.log("Failed")

    }
  },[])
  const submit=()=>{
   axios.post("http://node.dayuan0307.top:300/victy/lg",{
    name:username
   }).then((res)=>{
    const id = res.data.id
    console.log(id)
    //根据id判断是否登录成功
    switch(id){
      case "登录成功":
           window.localStorage.setItem("victyToke",res.data.token)
     window.localStorage.setItem("victyname",username)
     navigate("/victyhome")

      break;
      case "用户名不对":
     message.error("用户名不对")
      break;


    }



   }).catch((err)=>{
   message.info("用户名不对")
   window.localStorage.setItem("victyname",username)


   })
  }
  //通过victyname判断用户是否登录过
  useEffect(()=>{
    const  victyname = window.localStorage.getItem("victyname")
  if(victyname){
    navigate("/victyhome")
  }else{
    console.log("请先登录")

  }

  },[])
  return (
    <div>
       <div>
        <h1 style={{textAlign:'center'}}>第五城用户登录</h1>
       </div>
       <div style={{display:"flex", alignItems:"center",justifyContent:"center"}}>
       用户登录: <Input  value={username} onChange={(e)=>{setusername(e.target.value)}} style={{width:"300px"}}/>
       </div>
       <div style={{display:"flex", alignItems:"center",justifyContent:"center",marginTop:"15px"}}>
        <Button type="primary" onClick={submit}>登录</Button></div>
    </div>
  )
}
